package com.tech.zhidengxian.service.impl;

import com.tech.zhidengxian.mapper.OrderMapper;
import com.tech.zhidengxian.model.constant.OrderState;
import com.tech.zhidengxian.model.dataobject.OrderDO;
import com.tech.zhidengxian.model.dto.OrderDTO;
import com.tech.zhidengxian.model.dto.result.ResponseCode;
import com.tech.zhidengxian.model.dto.result.ReturnResult;
import com.tech.zhidengxian.model.dto.result.ReturnResultUtils;
import com.tech.zhidengxian.service.api.GoodsService;
import com.tech.zhidengxian.service.api.OrderService;
import com.tech.zhidengxian.util.CommonUtil;
import com.tech.zhidengxian.util.JacksonUtils;
import com.tech.zhidengxian.util.ResultAssert;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author Ljh
 * @date 2023/5/13 11:16
 * @description 订单业务逻辑类
 **/
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;
    /**
    *@author Ljh
    *@params [orderDTO]
    *@date 2023/5/2 11:27
    *@description 创建新订单
    *@return com.tech.zhidengxian.model.dto.OrderDTO
    **/
    @Override
    public OrderDTO createNewOrder(OrderDTO orderDTO) {
        orderDTO.setCreateTime(new Date());
        orderDTO.setUpdateTime(orderDTO.getCreateTime());
        orderDTO.setOrderState(OrderState.init);
        orderDTO.setOrderNumber(CommonUtil.createOrderNumber(orderDTO));
        OrderDO orderDO = JacksonUtils.convert(orderDTO, OrderDO.class);
        ResultAssert.assertGreaterThanZero(orderMapper.insert(orderDO), "DB异常");
        return JacksonUtils.convert(orderDO,OrderDTO.class);
    }
}
